﻿using System;
using System.Web.UI.WebControls;
using CivilRegistry.ControlledModel;
using CivilRegistry.Networking;

namespace CivilRegistry.Common
{
    public static class EmailHelper
    {
        public static void SendApplicationRequestNotification(string applicationType, User user, ref Label lblStatus)
        {
            try
            {
                EmailEngine emailEngine = new EmailEngine();
                emailEngine.To.Add(user.EmailAddress);
                emailEngine.Subject = Messages.EmailMessage.ApplicationSubject.Replace("<ApplicationType>", applicationType);
                emailEngine.Body = Messages.EmailMessage.ApplicationSent
                    .Replace("<User>", user.FirstName)
                    .Replace("<ApplicationType>", applicationType);
                if (emailEngine.Send())
                {
                    lblStatus.Text += " An email has been sent to your registered email address with more information.";
                }
                else
                {
                    lblStatus.Text += " Unable to send the email due to an internal error.";
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorLogger.Log(ex);
                lblStatus.Text += " Unable to send the email due to an internal error.";
            }
        }

        public static void SendApproveEmail(string applicationType, User user, ref Label lblStatus)
        {
            try
            {
                EmailEngine emailEngine = new EmailEngine();
                emailEngine.To.Add(user.EmailAddress);
                emailEngine.Subject = Messages.EmailMessage.ApplicationSubject.Replace("<ApplicationType>", applicationType);
                emailEngine.Body = Messages.EmailMessage.ApplicationApproved.Replace("<User>", user.FirstName).Replace("<ApplicationType>", applicationType);
                if (emailEngine.Send())
                {
                    lblStatus.Text += " An email has been sent to " + user.FirstName + "'s email address with more information.";
                }
                else
                {
                    lblStatus.Text += " Unable to send the email to " + user.FirstName + " due to an internal error.";
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorLogger.Log(ex);
                lblStatus.Text += " Unable to send the email to " + user.FirstName + " due to an internal error.";
            }
        }

        public static void SendDeactivationEmail(string applicationType, string deactivationButtonText, User user, string deactivationReason, ref Label lblStatus)
        {
            try
            {
                EmailEngine emailEngine = new EmailEngine();
                emailEngine.To.Add(user.EmailAddress);
                emailEngine.Subject = Messages.EmailMessage.ApplicationSubject.Replace("<ApplicationType>", applicationType);
                if (deactivationButtonText == "Re-Activate")
                {
                    emailEngine.Body = Messages.EmailMessage.ApplicationReActivated
                        .Replace("<User>", user.FirstName)
                        .Replace("<ApplicationType>", applicationType);
                }
                else
                {
                    emailEngine.Body = Messages.EmailMessage.ApplicationDeactivated
                        .Replace("<User>", user.FirstName)
                        .Replace("<ApplicationType>", applicationType)
                        .Replace("<Reason>", deactivationReason);
                }
                if (emailEngine.Send())
                {
                    lblStatus.Text += " An email has been sent to " + user.FirstName + "'s email address with more information.";
                }
                else
                {
                    lblStatus.Text += " Unable to send the email to " + user.FirstName + " due to an internal error.";
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorLogger.Log(ex);
                lblStatus.Text += " Unable to send the email to " + user.FirstName + " due to an internal error.";
            }
        }

        public static void SendDenyNotificationEmail(string applicationType, User user, string denyReason ,ref Label lblStatus)
        {
            try
            {
                EmailEngine emailEngine = new EmailEngine();
                emailEngine.To.Add(user.EmailAddress);
                emailEngine.Subject = Messages.EmailMessage.ApplicationSubject.Replace("<ApplicationType>", applicationType);
                emailEngine.Body = Messages.EmailMessage.ApplicationDenyed.Replace("<User>", user.FirstName)
                    .Replace("<ApplicationType>", applicationType).Replace("<Reason>", denyReason);
                if (emailEngine.Send())
                {
                    lblStatus.Text += " An email has been sent to " + user.FirstName + "'s email address with more information.";
                }
                else
                {
                    lblStatus.Text += " Unable to send the email to " + user.FirstName + " due to an internal error.";
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorLogger.Log(ex);
                lblStatus.Text += " Unable to send the email to " + user.FirstName + " due to an internal error.";
            }
        }

        public static void SendVerificationCode(User user, string verificationCode, ref Label lblStatus)
        {
            try
            {
                EmailEngine emailEngine = new EmailEngine();
                emailEngine.To.Add(user.EmailAddress);
                emailEngine.Subject = Messages.EmailMessage.VerificationCodeSubject;
                emailEngine.Body = Messages.EmailMessage.VerificationCode.Replace("<User>", user.FirstName)
                    .Replace("<VerificationCode>", verificationCode);
                if (emailEngine.Send())
                {
                    lblStatus.Text += " An email has been sent to " + user.FirstName + "'s email address with more information.";
                }
                else
                {
                    lblStatus.Text += " Unable to send the email to " + user.FirstName + " due to an internal error.";
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorLogger.Log(ex);
                lblStatus.Text += " Unable to send the email to " + user.FirstName + " due to an internal error.";
            }
        }

        public static void SendPassword(User user, ref Label lblStatus)
        {
            try
            {
                EmailEngine emailEngine = new EmailEngine();
                emailEngine.To.Add(user.EmailAddress);
                emailEngine.Subject = Messages.EmailMessage.NewPasswordSubject;
                emailEngine.Body = Messages.EmailMessage.NewPassword.Replace("<User>", user.FirstName)
                    .Replace("<UserName>", user.UserName)
                    .Replace("<Password>", user.Password);
                if (emailEngine.Send())
                {
                    lblStatus.Text += " An email has been sent to " + user.FirstName + "'s email address with more information.";
                }
                else
                {
                    lblStatus.Text += " Unable to send the email to " + user.FirstName + " due to an internal error.";
                }

            }
            catch (Exception ex)
            {
                Logger.ErrorLogger.Log(ex);
                lblStatus.Text += " Unable to send the email to " + user.FirstName + " due to an internal error.";
            }
        }
    }
}